<template>
  <el-form :model="ruleFormReg" :rules="rules" ref="ruleFormReg" label-position="left" label-width="0px" class="demo-ruleForm login-container register-container">
    <h3 class="title">企业注册</h3>
    <el-form-item prop="companyName">
      <el-input v-model="ruleFormReg.companyName" placeholder="企业名称"></el-input>
    </el-form-item>
    <el-form-item prop="namePass">
      <el-input v-model="ruleFormReg.namePass" placeholder="曾用名"></el-input>
    </el-form-item>
    <el-form-item prop="legalPerson">
      <el-input v-model="ruleFormReg.legalPerson" placeholder="法人代表"></el-input>
    </el-form-item>
    <el-form-item prop="username">
      <el-input v-model="ruleFormReg.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item prop="mobile">
      <el-input v-model="ruleFormReg.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item>
      <el-col :span="5">
        <el-form-item prop="country">
        <el-select v-model="ruleFormReg.country" placeholder="请选择国家">
          <el-option v-for="(item, key) in countries" :key="key" :label="item" :value="item"></el-option>
        </el-select>
        </el-form-item>
      </el-col>
      <el-col class="line" :span="2">-</el-col>
      <el-col :span="5">
        <el-form-item prop="province">
        <el-select v-model="ruleFormReg.province" placeholder="请选择省份">
          <el-option v-for="(item, key) in provinces" :key="key" :lable="item" :value="item"></el-option>
        </el-select>
        </el-form-item>
      </el-col>
      <el-col class="line" :span="1">-</el-col>
      <el-col :span="5">
        <el-form-item prop="city">
        <el-select v-model="ruleFormReg.city" placeholder="请选择城市">
          <el-option v-for="(item, key) in cities" :key="key" :lable="item" :value="item"></el-option>
        </el-select>
        </el-form-item>
      </el-col>
      <el-col class="line" :span="1">-</el-col>
      <el-col :span="5">
        <el-form-item prop="area">
        <el-select v-model="ruleFormReg.area" placeholder="请选择区县">
          <el-option v-for="(item, key) in areas" :key="key" :lable="item" :value="item"></el-option>
        </el-select>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item prop="address">
      <el-input v-model="ruleFormReg.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item prop="businessScope">
      <el-input type="textarea" v-model="ruleFormReg.businessScope" placeholder="经营范围" resize="none"></el-input>
    </el-form-item>
    <el-form-item prop="remarks">
      <el-input type="textarea" v-model="ruleFormReg.remarks" placeholder="备注" resize="none"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="ruleFormReg.password" placeholder="登录密码"></el-input>
    </el-form-item>
    <el-form-item prop="checkPassword">
      <el-input type="password" v-model="ruleFormReg.checkPassword" placeholder="确认密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="registerAction" :loading="loading">立即注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'Register',
  data () {
    return {
      loading: false,
      ruleFormReg: {
        companyName: '',
        namePass: '',
        legalPerson: '',
        username: '',
        password: '',
        checkPassword: '',
        mobile: '',
        country: '',
        province: '',
        city: '',
        region: '',
        address: '',
        businessScope: '',
        remarks: ''
      },
      rules: {
        companyName: [
          {required: true, message: '请输入企业名称', trigger: 'blur'},
          {max: 100, message: '企业名称不能超过100个字符', trigger: 'blur'}
        ],
        namePass: [
          {max: 100, message: '曾用名不能超过100个字符', trigger: 'blur'}
        ],
        legalPerson: [
          {required: true, message: '请输入法人代表', trigger: 'blur'},
          {max: 50, message: '法人代表不能超过50个字符', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 32, message: '用户名长度在 5 到 32 个字符', trigger: 'blur'}
        ],
        mobile: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '手机号格式错误', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 32, message: '登录密码长度在 6 到 32 个字符', trigger: 'blur'}
        ],
        checkPassword: [
          {required: true, message: '请输入确认密码', trigger: 'blur'},
          {validator: this.validateCheckPassword, trigger: 'blur'}
        ]
      },
      countries: ['中国', '美国', '俄罗斯', '日本', '德国', '英国'],
      provinces: ['浙江', '江苏', '河南', '安徽'],
      cities: ['杭州', '宁波', '温州', '嘉兴', '湖州', '绍兴', '台州', '丽水', '舟山'],
      areas: ['上城区', '下城区', '江干区', '拱野区', '西湖区', '滨江区', '余杭区']
    }
  },
  methods: {
    // 密码一致性验证
    validateCheckPassword (rule, value, callback) {
      if (value !== this.ruleFormReg.password) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    },
    registerAction () {
      this.$refs.ruleFormReg.validate((valid) => {
        if (!valid) return
        this.loading = true
        setTimeout(() => {
          this.loading = false
          this.$message({message: '注册成功', type: 'success'})
          this.$router.push('/login')
        }, 500)
      })
    }
  }
}

</script>

<style scoped>

</style>
